const https = require('https');
const fs = require('fs')
const path = require('path')
const { isFileExisted, fetchJSON } = require('../utils/netTools')

var options = {
	headers: {
		'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
	}//伪造请求头
}


class MoviesController {
	//正在热映
	async movieOnInfoList(req, res, next) {
		let myPath = path.join(__dirname, '../data/movieOnInfoList.json');
		const data = JSON.parse(fs.readFileSync(myPath, 'utf8'));
		res.json({
			code: 200,
			msg: "电影列表",
			data
		})
	}

	//电影信息
	async detailmovie(req, res, next) {
		const movieId = req.query.movieId || 1203734;

		const url = `https://m.maoyan.com/ajax/detailmovie?movieId=${movieId}`

		let myPath = path.join(__dirname, `../data/detailmovie/${movieId}.json`);

		let backupPath = path.join(__dirname, '../data/detailmovie.json');

		let msg = '电影信息'

		fetchJSON({
			url,
			myPath,
			backupPath,
			msg,
			res
		})
	}


	//影院信息
	async cinemaDetail(req, res, next) {
		const cinemaId = req.params.cinemaId || 890;

		const url = `https://m.maoyan.com/ajax/cinemaDetail?cinemaId=${cinemaId}`

		let myPath = path.join(__dirname, `../data/cinemaDetail/${cinemaId}.json`);

		let backupPath = path.join(__dirname, '../data/cinemaDetail.json');

		let msg = '影院信息'

		fetchJSON({
			url,
			myPath,
			backupPath,
			msg,
			res
		})
	}

	//城市列表
	async cities(req, res, next) {
		let myPath = path.join(__dirname, '../data/cities.json');
		const data = JSON.parse(fs.readFileSync(myPath, 'utf8'));
		res.json({
			code: 200,
			msg: "城市列表",
			data
		})
	}

	//影院列表(弃)
	async filterCinemas(req, res, next) {
		let responseString = '';
		https.get('https://m.maoyan.com/ajax/filterCinemas?ci=70', (result) => {

			result.setEncoding('utf-8');

			result.on('data', function (data) {
				responseString += data;
			});

			result.on('end', function () {
				let data;
				try {
					data = JSON.parse(responseString);
					console.log(responseString);
				} catch {
					let myPath = path.join(__dirname, '../data/filterCinemas.json');
					data = JSON.parse(fs.readFileSync(myPath, 'utf8'));
				}
				res.json({
					code: 200,
					msg: "影院列表",
					data
				})
			});

		}).on('error', (e) => {
			console.error(e);
		});
	}

	//即将上映
	async comingList(req, res, next) {
		let myPath = path.join(__dirname, '../data/comingList.json');

		const data = JSON.parse(fs.readFileSync(myPath, 'utf8'));
		res.json({
			code: 200,
			msg: "电影列表",
			data
		})
	}

	//影院列表
	async moreCinemas(req, res, next) {
		const ci = req.query.ci || 70

		const { movieId } = req.query

		const url = `https://i.maoyan.com/ajax/moreCinemas?ci=${ci}`

		let myPath = path.join(__dirname, `..//data/moreCinemas/${ci}.json`);

		let backupPath = path.join(__dirname, '../data/moreCinemas.json');

		let msg = '影院列表'

		fetchJSON({
			url,
			myPath,
			backupPath,
			msg,
			res
		})
	}

	async getMovieSchedule(req, res, next) {
		const { movieId } = req.query
		const moreCinemasPath = path.join(__dirname, `../data/moreCinemas.json`);
		const moreCinemas = JSON.parse(fs.readFileSync(moreCinemasPath, 'utf-8'))

		const filtered = moreCinemas.cinemas.cinemas.filter(cinema => {
			const cinemaId = cinema.id
			const cinemaDetailPath = path.join(__dirname, `../data/cinemaDetail/${cinemaId}.json`);
			try {
				const cinema = JSON.parse(fs.readFileSync(cinemaDetailPath, 'utf-8'));
				if (cinema.showData.movies.some(movie => movie.id == movieId)) {
					return true
				}
			} catch {
				console.log('error');
			}
		})

		moreCinemas.cinemas.cinemas = filtered

		res.json({
			code: 200,
			msg: '注册成功',
			data: moreCinemas
		})
	}
}

module.exports = new MoviesController()